package com.stylefeng.guns.modular.cash.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.BaseModelWarpper;
import com.stylefeng.guns.core.common.annotion.BussinessLog;
import com.stylefeng.guns.core.common.constant.cache.Cache;
import com.stylefeng.guns.core.common.constant.cache.CacheKey;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.modular.cash.service.IDetailsService;
import com.stylefeng.guns.modular.system.model.Details;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * 现金收支类型管理控制器
 *
 * @author fengshuonan
 * @Date 2018-07-03 11:53:35
 */
@Controller
@RequestMapping("/details")
public class DetailsController extends BaseController {

    private String PREFIX = "/cash/details/";

    @Autowired
    private IDetailsService detailsService;

    /**
     * 跳转到现金收支类型管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "details.html";
    }

    /**
     * 跳转到添加现金收支类型管理
     */
    @RequestMapping("/details_add")
    public String detailsAdd() {
        return PREFIX + "details_add.html";
    }

    /**
     * 跳转到修改现金收支类型管理
     */
    @RequestMapping("/details_update/{detailsId}")
    public String detailsUpdate(@PathVariable Integer detailsId, Model model) {
        Details details = detailsService.selectById(detailsId);
        model.addAttribute("item",details);
        LogObjectHolder.me().set(details);
        return PREFIX + "details_edit.html";
    }

    /**
     * 获取现金收支类型管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String  name) throws Exception {
        EntityWrapper<Details> wrapper = new EntityWrapper<Details>();
        //组装查询条件
        if(StringUtils.isNotBlank(name)) {
            wrapper.like("name", name);
        }
        return new BaseModelWarpper(detailsService.selectList(wrapper),Details.class).warpToObj();
    }

    /**
     * 新增现金收支类型管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @BussinessLog(value = "新增现金收支类型管理")
    public Object add(Details details) {
        details.setCreatdate(new Date());
        detailsService.insert(details);
        return SUCCESS_TIP;
    }

    /**
     * 删除现金收支类型管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @BussinessLog(value = "删除现金收支类型管理")
    @CacheEvict(value= Cache.CONSTANT, key="'" + CacheKey.DETAILS_CODE+"'+#detailsId")
    public Object delete(@RequestParam Integer detailsId) {
        detailsService.deleteById(detailsId);
        return SUCCESS_TIP;
    }

    /**
     * 修改现金收支类型管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @BussinessLog(value = "修改现金收支类型管理")
    @CacheEvict(value= Cache.CONSTANT, key="'" + CacheKey.DETAILS_CODE+"'+#details.getId()")
    public Object update(Details details) {
        details.setUpdatedate(new Date());
        detailsService.updateById(details);
        return SUCCESS_TIP;
    }

    /**
     * 现金收支类型管理详情
     */
    @RequestMapping(value = "/detail/{detailsId}")
    @ResponseBody
    public Object detail(@PathVariable("detailsId") Integer detailsId) {
        return detailsService.selectById(detailsId);
    }
}
